package com.zq.thread.volatile1;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * @Author: zhaoqi
 * @Date: 11/23/2019 11:12 AM
 * @Version 1.0   volatile并不能保证线程安全   AtomicInteger可以
 */




public class VolitileInc implements Runnable {

//    private static volatile int count = 0;

    private static volatile AtomicInteger count = new AtomicInteger();

    @Override
    public void run() {
        for (int i = 0; i < 1000000; i++) {
//            count++;
            count.incrementAndGet();
            System.out.println(count);
        }
        System.out.println("-----" +count);
    }

    public static void main(String[] args) throws InterruptedException {
        VolitileInc volitileInc = new VolitileInc();
        Thread t1 = new Thread(volitileInc,"t1");
        Thread t2 = new Thread(volitileInc,"t2");
        Thread t3 = new Thread(volitileInc,"t2");
        t1.start();
        t3.start();
        t2.start();
        t1.join();
        t2.join();
        t3.join();
        /*for (int i = 0; i < 1000; i++) {
            ///count++;
            count.incrementAndGet();

        }*/
        System.out.println("最终的count： " + count);
    }

}
